package com.jason.concurrent.commuicate;

import com.sun.org.apache.xml.internal.resolver.readers.TR9401CatalogReader;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author  yslai
 * @Date   16:36
 * @Description 交替打印输出内容
 */


class print{

    private  int number =0;
    ReentrantLock lock = new ReentrantLock();

    public synchronized void printJi() throws InterruptedException {
        if (number%2==0)
        {
            this.wait();
        }
        System.out.println(Thread.currentThread().getName()+"::"+number++);
        this.notifyAll();

    }
    public synchronized void printOu() throws InterruptedException {
        if (number%2==1)
        {
            this.wait();
        }
        System.out.println(Thread.currentThread().getName()+"::"+number++);
        this.notifyAll();

    }


}
public class Sync {

    public static void main(String[] args) {
        print print = new print();
        Thread aa = new Thread(() -> {

            for (int i = 0; i < 50; i++) {
                try {
                    print.printJi();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }, "aa");
        Thread bb = new Thread(() -> {

            for (int i = 0; i < 50; i++) {
                try {
                    print.printOu();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }, "bb");
        aa.start();
        bb.start();
    }
}
